package com.code.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.code.dao.GeneratorDao;
import com.code.form.CodeGenForm;
import com.code.utils.GenUtils;
import com.code.utils.PageUtils;
import com.code.utils.Query;

import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.ByteArrayOutputStream;
import java.util.List;
import java.util.Map;
import java.util.zip.ZipOutputStream;

/**
 * 代码生成器
 * 
 * @author hyl
 * @date 2019/1/11
 */
@Service
public class SysGeneratorService {
	@Autowired
	private GeneratorDao generatorDao;

	public PageUtils queryList(Query query) {
		Page<?> page = PageHelper.startPage(query.getPage(), query.getLimit());
		List<Map<String, Object>> list = generatorDao.queryList(query);

		return new PageUtils(list, (int) page.getTotal(), query.getLimit(), query.getPage());
	}

	public Map<String, String> queryTable(String tableName) {
		return generatorDao.queryTable(tableName);
	}

	public List<Map<String, String>> queryColumns(String tableName) {
		return generatorDao.queryColumns(tableName);
	}

	public byte[] generatorCode(String[] tableNames) {
		ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
		ZipOutputStream zip = new ZipOutputStream(outputStream);

		for (String tableName : tableNames) {
			// 查询表信息
			Map<String, String> table = queryTable(tableName);
			// 查询列信息
			List<Map<String, String>> columns = queryColumns(tableName);
			// 生成代码
			GenUtils.generatorCode(table, columns);
		}
		IOUtils.closeQuietly(zip);
		return outputStream.toByteArray();
	}

	/*public void generatorCode(CodeGenForm form) {
		String[] tableNames = form.getTables().split(",");
		for (String tableName : tableNames) {
			// 查询表信息
			Map<String, String> table = queryTable(tableName);
			// 查询列信息
			List<Map<String, String>> columns = queryColumns(tableName);
			// 生成代码
			GenUtils.generatorCode(form.getMainPath(), form.getModuleName(), form.getPackageName(), table, columns);
		}
	}*/
    public byte[] generatorCode(CodeGenForm form) {
        String[] tableNames = form.getTables().split(",");

        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        ZipOutputStream zip = new ZipOutputStream(outputStream);

        for (String tableName : tableNames) {
            // 查询表信息
            Map<String, String> table = queryTable(tableName);
            // 查询列信息
            List<Map<String, String>> columns = queryColumns(tableName);
            // 生成代码
            GenUtils.generatorCode(form.getMainPath(), form.getModuleName(), form.getPackageName(), table, columns,
                    zip);
        }
        IOUtils.closeQuietly(zip);

        return outputStream.toByteArray();
    }
}
